(function () {
  new Vue({
    el: '#clauseChange',
    data() {
      return {
        id: hlUrlUtil.GetQueryString('id'),//合同ID
        code: hlUrlUtil.GetQueryString('code'),//变更code
        condition: hlUrlUtil.GetQueryString('condition'),//变更内容
        addFlag: false,
        addType: '', //添加类型
        projectId: '',
        buildingList: [], // 楼宇列表
        selectBuildingId: [], // 选中的楼宇Id
        selectBuildingItem: '', // 选中的楼宇
        floorList: [], // 楼层列表
        selectFloorId: [], // 选中的楼层Id
        selectFloorItem: '', // 选中的楼层
        unitList: [], // 单元列表
        selectUnitId: [],//选中的单元Id
        selectUnitItem: '',//选中的单元
        parkList: [], //车位列表
        stationList: [], //工位列表

        selectStation: [],//选中工位列表（展示）在工位选择模块
        selectPark: [], //选中车位列表（展示）在车位选择模块
        selectUnit: [], //选中单元列表（展示）在单元选择模块

        contractDetail: {
          selectStationList: [], //选中工位列表（展示）在工位选择模块
          selectParkList: [], //确认选中车位 在车位选择模块
          selectUnitList: [], //确认选中单元 在单元选择模块
        },//合同详情

        intent: {
          id: null,
          intentDeductibleAmount: null
        },

      }
    },
    computed: {
      _showSelectUnit_() {
        let arr = []
        this.selectUnit.map(item => {
          arr.push(item.unit_id)
        })
        return arr
      },
      _showSelectPark_() {
        let arr = []
        this.selectPark.map(item => {
          item.stops.map(single => {
            arr.push(single.stop_id)
          })
        })
        return arr
      },
      _showSelectStation_() {
        let arr = []
        this.selectStation.map(item => {
          item.stops.map(single => {
            arr.push(single.stop_id)
          })
        })
        return arr
      }
    },
    methods: {
      // 添加单元/车位
      addHouse(val) {
        this.addType = val
        this.addFlag = true
        if (val == 'unit') {
          this.getBuildings()
          this.selectBuildingId = []
          this.selectBuildingItem = ''
          this.floorList = []
          this.selectFloorId = []
          this.selectFloorItem = ''
          this.unitList = []
          this.selectUnit = JSON.parse(JSON.stringify(this.contractDetail.selectUnitList))
        }
        if (val == 'park') {
          this.getBuildings()
          this.selectBuildingId = []
          this.selectBuildingItem = ''
          this.floorList = []
          this.selectFloorId = []
          this.selectFloorItem = ''
          this.unitList = []
          this.selectUnitId = []
          this.selectUnitItem = ''
          this.parkList = []
          this.stationList = []
          this.unitList = []
          this.selectPark = JSON.parse(JSON.stringify(this.contractDetail.selectParkList))
        }
        if (val == 'station') {
          this.getBuildings()
          this.selectBuildingId = []
          this.selectBuildingItem = ''
          this.floorList = []
          this.selectFloorId = []
          this.selectFloorItem = ''
          this.unitList = []
          this.selectUnitId = []
          this.selectUnitItem = ''
          this.parkList = []
          this.stationList = []
          this.selectStation = JSON.parse(JSON.stringify(this.contractDetail.selectStationList))
        }
      },
      //获取楼宇
      getBuildings: function () {
        var that = this
        hlhttp.get({
          url: 'project/buildings',
          data: {
            project_id: this.contractDetail.projectId
          },
          success: function (res) {
            res.map(item => {
              item.code = item.buildingId
            })
            that.buildingList = res
          }
        })
      },
      //选择楼宇并获取楼层:
      setBuilding: function (val) {
        this.selectBuildingItem = val[0]
        var that = this
        hlhttp.get({
          url: 'building/floors',
          data: {
            project_id: this.contractDetail.projectId,
            building_id: this.selectBuildingId + '',
          },
          success: function (res) {
            res.map(item => {
              item.name = item.floorName
              item.code = item.floorId
            })
            that.selectFloorId = []
            that.unitList = []
            that.floorList = res
          }
        })
      },
      // 选择楼层
      setFloor(val) {
        this.selectFloorItem = val[0]
        if (this.addType == 'park') {
          this.selectUnitId = []
          this.parkList = []
          this.getUnit()
        }
        if (this.addType == 'station') {
          this.selectUnitId = []
          this.getUnit()
        }
      },
      // 选择单元
      setUnit(val) {
        this.selectUnitItem = val[0]
      },
      //获取单元
      getUnit: function () {
        if (this.addType == 'unit') {
          if (!this.searchCheck()) {
            return
          }
        }
        var that = this
        hlhttp.get({
          url: 'floor/units',
          data: {
            project_id: this.contractDetail.projectId,
            building_id: this.selectBuildingId + '',
            floor_id: this.selectFloorId + '',
          },
          success: function (res) {
            res.map(item => {
              item.name = item.unitName
              item.code = item.unitId
            })
            let arr = []
            if (that.addType == 'unit') {
              arr = res
            }
            if (that.addType == 'park') {
              res.map(item => {
                if (item.unitType == 'ST') {
                  arr.push(item)
                }
              })
            }
            if (that.addType == 'station') {
              res.map(item => {
                if (item.unitType == 'WP') {
                  arr.push(item)
                }
              })
            }
            that.unitList = arr
          }
        })
      },
      // 获取车位/工位
      getPark_station() {
        if (!this.searchCheck()) {
          return
        }
        let that = this
        hlhttp.get({
          url: 'units/positions',
          data: {
            unit_id: this.selectUnitId + ''
          },
          success(res) {
            that.parkList = res
          }
        })
      },
      // 选择单元
      selectUnitIds(obj) {
        let flag = true
        let i = ''
        this.selectUnit.some((item, index) => {
          if (item.unit_id == obj.unitId) {
            flag = false
            i = index
          }
        })
        if (flag) {
          this.selectUnit.push({
            building_id: this.selectBuildingId + '',
            building_name: this.selectBuildingItem.name,
            floor_id: this.selectFloorId + '',
            floor_num: this.selectFloorItem.floor,
            floor_name: this.selectFloorItem.floorName,
            unit_id: obj.unitId,
            unit_name: obj.unitName,
            unit_acreage: obj.acreage,
						business_type: obj.businessType
          })
        } else {
          this.selectUnit.splice(i, 1)
        }
      },
      // 选择车位/工位
      selectParkIds_stations(obj) {
        // debugger
        console.log(obj)

        let arr = []
        if (this.addType == 'park') {
          arr = this.selectPark
        } else {
          arr = this.selectStation
        }
        console.log(arr)
        let flag = true
        let i = ''
        let n = ''
        arr.some((item, index) => {
          if (item.unit_id == this.selectUnitId + '') {
            flag = false
            i = index
            item.stops.some((item0, index0) => {
              if (item0.stop_id == obj.positionId) {
                n = index0 + ''
              }
            })
          }
        })
        if (flag) {
          arr.push({
            building_id: this.selectBuildingId + '',
            building_name: this.selectBuildingItem.name,
            floor_id: this.selectFloorId + '',
            floor_num: this.selectFloorItem.floor,
            floor_name: this.selectFloorItem.floorName,
            unit_id: this.selectUnitId + '',
            unit_name: this.selectUnitItem.unitName,
            unit_acreage: this.selectUnitItem.acreage,
            stops: [{
              stop_id: obj.positionId,
              stop_name: obj.positionName,
							business_type: obj.businessType
            }]
          })
        } else {
          if (n) {
            arr[i].stops.splice(n, 1)
          } else {
            arr[i].stops.push({
              stop_id: obj.positionId,
              stop_name: obj.positionName,
							business_type: obj.businessType
            })
          }
        }
      },
      // 确定添加单元
      sureAdd() {
        if (this.addType == 'unit') {
          this.contractDetail.selectUnitList = JSON.parse(JSON.stringify(this.selectUnit))
          this.addFlag = false
        }
        if (this.addType == 'park') {
          this.contractDetail.selectParkList = JSON.parse(JSON.stringify(this.selectPark))
          this.addFlag = false
        }
        if (this.addType == 'station') {
          this.contractDetail.selectStationList = JSON.parse(JSON.stringify(this.selectStation))
          this.addFlag = false
        }

      },
      // 上一步/下一步
      previous() {
        window.sessionStorage.setItem('CONTRACT_DETAIL', JSON.stringify(this.contractDetail))
        window.changePage.pageIndex--
      },
      next() {
        window.sessionStorage.setItem('CONTRACT_DETAIL', JSON.stringify(this.contractDetail))
        window.changePage.pageIndex++
      },
      // 查询之前的校验
      searchCheck() {
        if (this.addType == 'unit') {
          if (!this.selectBuildingId.length) {
            hl.toast('请选择楼宇')
            return false
          }
          if (!this.selectFloorId.length) {
            hl.toast('请选择楼层')
            return false
          }
        }
        if (this.addType == 'park') {
          if (!this.selectBuildingId.length) {
            hl.toast('请选择楼宇')
            return false
          }
          if (!this.selectFloorId.length) {
            hl.toast('请选择楼层')
            return false
          }
          if (!this.selectUnitId.length) {
            hl.toast('请选择单元')
            return false
          }
        }
        return true
      }
    },
    mounted() {

      this.contractDetail = JSON.parse(window.sessionStorage.getItem('CONTRACT_DETAIL'))
			console.log(this.contractDetail)
      this.selectStation = this.contractDetail.selectStationList
      this.selectPark = this.contractDetail.selectParkList
        this.selectUnit = this.contractDetail.selectUnitList
    },
  })
}())